#include "State_Movie.h"
//#include "State_GamePlay.h"

extern IDirectInputDevice8		*mJoystick[2]; // This will be changed to an area later...
extern DIJOYSTATE2				js;
//extern 	int prevJSButtons[12];
//extern 	int prevJSButtons2[12];
//extern 	int prevJS;
extern 	ID3DXSprite* mSprite;

State_Movie* State_Movie::init = 0;

State_Movie::State_Movie()
{
	HR(D3DXCreateTextureFromFile(gd3dDevice, "MazeMenuTest01.png", &mBkgdTex));
	mBkgdCenter = D3DXVECTOR3(640.0f, 400.0f, 0.0f);
	CoInitialize(NULL);
	IGraphBuilder*	g_pGraphBuilder = NULL;
	IMediaControl*	g_pMediaControl = NULL;
	IMediaEventEx*	g_pMediaEvent = NULL;
	IMediaPosition*	g_pMediaPosition = NULL;

}

void State_Movie::Enter()
{
//	cout << "State_Menu has been entered\n";
	choice = '0';


}

void State_Movie::Execute(D3DApp* g)
{
			
	static float cursorDelay = 0.0f;
	static float timeAccum = 0.0f;
	static int prevJS = 0;
	mJoystick[0]->GetDeviceState(sizeof(DIJOYSTATE2), &js);
		
	if(((js.rgdwPOV[0] != -1) && js.rgdwPOV[0] == 0) && (js.rgdwPOV[0] ^ prevJS))
	{
		//MessageBox(gd3dApp->getMainWindowHandle(), "1st", "Error", 0);
		//MessageBox(gd3dApp->getMainWindowHandle(), "1st", "Error", 0);
	}

	else if(((js.rgdwPOV[0] != -1) && js.rgdwPOV[0] == 9000) && (js.rgdwPOV[0] ^ prevJS))
	{
		//MessageBox(gd3dApp->getMainWindowHandle(), "2nd", "Error", 0);
	}

	else if(((js.rgdwPOV[0] != -1) && js.rgdwPOV[0] == 18000) && (js.rgdwPOV[0] ^ prevJS))
	{
		//MessageBox(gd3dApp->getMainWindowHandle(), "3rd", "Error", 0);
	}

	else if(((js.rgdwPOV[0] != -1) && js.rgdwPOV[0] == 27000) && (js.rgdwPOV[0] ^ prevJS))
	{
		//MessageBox(gd3dApp->getMainWindowHandle(), "4th", "Error", 0);
	}
	else if((js.rgbButtons[0] & 0x80) && (prevJSButtons[0] ^ js.rgbButtons[0]))
	{
		gd3dApp->setState(State_Menu::Instance());
		g_pMediaControl->Stop();	
	}

	else if(js.rgbButtons[1] & 0x80 && (prevJSButtons[1] ^ js.rgbButtons[1]))
	{
	//InitDirectShow();
		InitDirectShow();
		CreateGraph("test.wmv");
		g_pMediaControl->Run();	
		choice = '1';
	}
	else if(js.rgbButtons[2] & 0x80 && (prevJSButtons[2] ^ js.rgbButtons[2]))
	{
        
	}

	else if(js.rgbButtons[3] & 0x80 && (prevJSButtons[3] ^ js.rgbButtons[3]))
	{

	}
	else if(js.rgbButtons[4] & 0x80 && (prevJSButtons[4] ^ js.rgbButtons[4]))
	{
		//PostQuitMessage(0);
	}

	if(!(js.rgdwPOV[0] == 4500 || js.rgdwPOV[0] == 13500 || js.rgdwPOV[0] == 22500 || js.rgdwPOV[0] == 31500))
			//prevJS = js.rgdwPOV[0];

	for(int i = 0; i < 12; i++)
	{
		//prevJSButtons[i] = js.rgbButtons[i];
	}



	Draw();
}

void State_Movie::Exit()
{
	CleanUpDirectShow();
}

void State_Movie::Draw()
{
	if (choice == '1') 
	{
	OnGraphEvent();
	}

    // Now we can clear just view-port's portion of the buffer to red...
   HR(gd3dDevice->Clear(0, 0, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, 0xffffffff, 1.0f, 0));

    HR(gd3dDevice->BeginScene());



    //draw stuff here
	HR(mSprite->Begin(D3DXSPRITE_OBJECTSPACE | D3DXSPRITE_DONOTMODIFY_RENDERSTATE));

	D3DXMATRIX texScaling;
	D3DXMatrixScaling(&texScaling, 1.0f, -1.0f, 0.0f);
	HR(gd3dDevice->SetTransform(D3DTS_TEXTURE0, &texScaling));

	D3DXMATRIX T, S;
	D3DXMatrixTranslation(&T, -260.0f, -140.0f, 0.0f);
	D3DXMatrixScaling(&S, 0.68f, 1.0f, 0.0f);
	HR(mSprite->SetTransform(&(S*T)));

	//draw the menu
	HR(mSprite->Draw(mBkgdTex, 0, &mBkgdCenter, 0, D3DCOLOR_XRGB(255, 255, 255)));
	HR(mSprite->Flush());

	//restore the texture coordinate scaling transform. mirror texture 
	//coordinates with respect to y-axis so the texture is mapped correctly onto the sprite
	D3DXMatrixScaling(&texScaling, 1.0f, -1.0f, 0.0f);
	HR(gd3dDevice->SetTransform(D3DTS_TEXTURE0, &texScaling));

	//anything that needs to be drawn should be above this line and below the mSprite->Begin line
	HR(mSprite->End());

	HR(gd3dDevice->EndScene());
	


	//present the back buffer
	HR(gd3dDevice->Present(0, 0, 0, 0));
}

State_Movie* State_Movie::Instance()
{
	if(init == 0)
	{
		init = new State_Movie();
	}
	return init;
}



void State_Movie::InitDirectShow()
{
	HRESULT hr;
	
	hr = CoInitialize(NULL);

	hr = CoCreateInstance(CLSID_FilterGraph, NULL, CLSCTX_INPROC_SERVER, IID_IGraphBuilder,
        (void**)&g_pGraphBuilder);

	g_pGraphBuilder->QueryInterface(IID_IMediaControl, (void**)&g_pMediaControl);
	g_pGraphBuilder->QueryInterface(IID_IMediaEvent, (void**)&g_pMediaEvent);
	g_pGraphBuilder->QueryInterface(IID_IMediaPosition, (void**)&g_pMediaPosition);

	g_pMediaEvent->SetNotifyWindow(NULL, 0, 0); //WM_GRAPHEVENT
	g_pMediaEvent->SetNotifyFlags(0);	// turn on notifications
}

void State_Movie::CreateGraph(char* filename)
{
	int	    length;		// length of filename
	WCHAR*	wfilename;	// where we store WCHAR version of filename
    HRESULT hr;

    // create graphbuilder's log file;  we must give it a full path or
    // else it will create the log file in the directory the media file is
    // located in
    //sprintf(logfilename, "%s\\graph.log", g_ExecuteDir);
    //logfile = CreateFile(logfilename, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS,
  //      FILE_ATTRIBUTE_NORMAL, NULL);
//    g_pGraphBuilder->SetLogFile(logfile);

    // attempt to build a graph
	length = strlen(filename)+1;
	wfilename = new WCHAR[length];
	MultiByteToWideChar(CP_ACP, 0, filename, -1, wfilename, length);
    hr = g_pGraphBuilder->RenderFile(wfilename, 0);
  //  CloseHandle(logfile);

}

void State_Movie::CleanUpDirectShow()
{
    HELPER_RELEASE(g_pMediaPosition);
    HELPER_RELEASE(g_pMediaEvent);
    HELPER_RELEASE(g_pMediaControl);
    HELPER_RELEASE(g_pGraphBuilder);
}

// called when directshow events occur
void State_Movie::OnGraphEvent()
{
	long EventCode, Param1, Param2;
	while (g_pMediaEvent->GetEvent(&EventCode, &Param1, &Param2, 0) != E_ABORT)
	{
		switch (EventCode)
		{
        case EC_COMPLETE:            
            // here when media is completely done playing
			g_pMediaControl->Stop();
			g_pMediaControl->Release();
			//gd3dApp->setState(State_Menu::Instance());
			//CleanUpDirectShow();
		//	choice = '2';
            break;
		default:
			break;
		}	
		g_pMediaEvent->FreeEventParams(EventCode, Param1, Param2);
	}
}
